package org.example.day03;

/**
 * 第二章节2.1.1:方法内的变量为线程安全
 *      多个线程对同一个对象中的实例变量并发访问时发生，产生后果就是"脏读"
 */
public class MyThread extends Thread{
    public void addI(String username){
        try {
            int num = 0;
            if(username.equals("a")){
                num = 100;
                System.out.println("a set over");
            }else {
                num = 200;
                System.out.println("b set over");
            }
            System.out.println(username + " num = " + num);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MyThread m3 = new MyThread();
        ThreadA tA = new ThreadA(m3);
        tA.start();
        ThreadB tB = new ThreadB(m3);
        tB.start();
    }
}

class ThreadA extends Thread{
    private MyThread m3;
    public ThreadA(MyThread m3){
        this.m3 = m3;
    }

    @Override
    public void run() {
        m3.addI("a");
    }
}

class ThreadB extends Thread{
    private MyThread m3;
    public ThreadB(MyThread m3){
        this.m3 = m3;
    }

    @Override
    public void run() {
        m3.addI("b");
    }
}